Each Function
object has apply()
, call()
, and bind()
methods, which are used to call functions in a specific scope, effectively setting the value of the this
object within the function body to extend the scope in which the function runs.
funct.apply(thisArg, [argsArray])
thisArg
: Required, the value of this
to be used when the funct
function is running. this
may not be the actual value seen by the method. If this function is in non-strict mode, specifying null
or undefined
will automatically be replaced by a reference to the global object and primitive values will be wrapped.
argsArray
: Optional, pass an array-like object containing the arguments to be passed to the funct
function. If the value of this parameter is null
or undefined
, it indicates that no arguments need to be passed.
The implementation approach is similar to Function.prototype.apply()
, by attaching the _apply()
method to the Function.prototype
, allowing function objects to directly call it. When calling funct._apply()
, the this
within the _apply()
method refers to the funct
object. This funct
object is assigned to a property of the object to be bound, and the funct
is then called using the object to be bound, thus achieving the this
pointer pointing to the object to be bound. For handling the arguments, the Spread
operator from ES6
is used to expand the array and pass it as arguments.
funct.call(thisArg[, arg1[, arg2[, ...]]])
thisArg
: Required, the value of this
to be used when the funct
function is running. this
may not be the actual value seen by the method. If this function is in non-strict mode, specifying null
or undefined
will automatically be replaced by a reference to the global object and primitive values will be wrapped.
arg1, arg2, ...
: Optional, the specified list of arguments.
The implementation approach is similar to Function.prototype.call()
, by attaching the _call()
method to the Function.prototype
, allowing function objects to directly call it. When calling funct._call()
, the this
within the _call()
method refers to the funct
object. This funct
object is assigned to a property of the object to be bound, and the funct
is then called using the object to be bound, thus achieving the this
pointer pointing to the object to be bound. For handling the arguments, the Rest
operator from ES6
is used to accept the remaining parameters, and the Spread
operator from ES6
is used to expand the array as arguments.
funct.bind(thisArg[, arg1[, arg2[, ...]]])
thisArg
: Required, the value passed as the this
parameter when calling the bound function, ignored if the bound function is constructed using the new
operator, when provided as a callback, any primitive value passed as thisArg
will be converted to an object, if the parameter list of the bind
function is empty, or thisArg
is null or undefined, the this
of the execution scope will be considered the thisArg
of the new function.
arg1, arg2, ...
: Optional, parameters that are preset into the parameter list of the bound function when the target function is called.
The implementation approach is similar to Function.prototype.bind()
. Similarly, the _bind()
method is mounted on Function.prototype
, allowing function objects to be called directly. By leveraging the lexical binding of this
value with arrow functions, it returns a function with a specified this
. If arrow functions are not used, the this
value can also be assigned to a closed variable to construct a closure, followed by an implementation similar to the apply
method to bind this
to the specified object.